package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealExistException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: HF62sky
 * @package: com.sky.service.impl
 * @className: SetmealServiceImpl
 * @author: SunGang
 * @description: TODO
 * @date: 2023/12/6 11:14
 * @version: 1.0
 */
@Service
@RequiredArgsConstructor
public class SetmealServiceImpl implements SetmealService {
    private final SetmealMapper setmealMapper;
    private final SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //先校验
        Integer countByName = setmealMapper.selectCountByName(setmealDTO.getName());
        if (countByName > 0) {
            throw new SetmealExistException(MessageConstant.SETMEAL_EXIST);
        }
        //添加到套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);
        //添加到套餐和菜单关系表
        addToSetmealDish(setmealDTO, setmeal);
    }

    /**
     * @param setmealPageQueryDTO:
     * @return PageResult
     * @author SunGang
     * @description 套餐分页查询
     * @date 2023/12/6 14:03
     */
    @Override
    public PageResult queryByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(setmealPageQueryDTO.getStatus());
        setmeal.setCategoryId(setmealPageQueryDTO.getCategoryId());
        setmeal.setName(setmealPageQueryDTO.getName());
        Page<Setmeal> setmeals = setmealMapper.selectQueryPage(setmeal);
        long total = setmeals.getTotal();
        List<SetmealVO> setmealVOs = setmeals.stream().map(setmeal1 -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal1, setmealVO);
            return setmealVO;
        }).collect(Collectors.toList());
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(setmealVOs);
        return pageResult;
    }

    @Override
    public void modifyStatus(Integer status, Long id) {
        setmealMapper.updateStatusById(status, id);
    }

    @Override
    @Transactional
    public void deleteSetmeal(List<String> ids) {
        Integer countByIds = setmealMapper.selectStatusCountByIds(ids, StatusConstant.ENABLE);
        if (countByIds > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public SetmealVO findById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        setmealVO.setCategoryId(Long.parseLong(setmeal.getCategoryId().toString()));
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void modifySetmeal(SetmealDTO setmealDTO) {
        //先校验
        Integer countByName = setmealMapper.selectCountByNameNotId(setmealDTO.getName(),setmealDTO.getId());
        if (countByName > 0) {
            throw new SetmealExistException(MessageConstant.SETMEAL_EXIST);
        }
        //修改数据添加到套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateById(setmeal);
        //删除套餐数据
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        addToSetmealDish(setmealDTO, setmeal);
    }
/**
 * @param setmealDTO:
 * @param setmeal:
 * @return void
 * @author SunGang
 * @description 添加数据到关系表
 * @date 2023/12/6 18:30
 */
    private void addToSetmealDish(SetmealDTO setmealDTO, Setmeal setmeal) {
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.insertToSetmealDish(setmealDishes);
    }
}
